﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using mvc6.Models;

namespace mvc6.Controllers
{
    public class DbController : Controller
    {

        private readonly DbContext _dbContext;

        public DbController(DbContext dbContext)
        {
            this._dbContext = dbContext;
        }

        public IActionResult Index()
        {
            using(_dbContext)
            {
                MyDbContext context = (MyDbContext)_dbContext;

                //Company company = new Company()
                //{

                //    Name = "yida"
                //};

                //context.Add<Company>(company);
                //context.SaveChanges();

                DbSet<Company> companys = context.Companys;
                Company? company1 = companys.OrderBy(e => e.Id).Reverse().FirstOrDefault<Company>();
                company1.Name = "Hello";
                context.Update<Company>(company1);
                context.SaveChanges();

            }


            return View();
        }
    }
}
